package com.lzx.adx.report.util.helper;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.baidu.BaiduRequest;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class WphHelper {


    public static String handWphUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new LinkedHashMap<>();
        params.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum())?reportDTO.getImeiSum():"");
        params.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid())?reportDTO.getOaid():"");
        params.put("oaid_md5", StringUtils.isNotBlank(reportDTO.getOaidSum())?reportDTO.getOaidSum():"");
        params.put("device_type", 2);
        params.put("click_time",String.valueOf(System.currentTimeMillis()));
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("click_ip",reportDTO.getIp());
        params.put("user_agent",reportDTO.getUser_agent());
        if(StringUtils.isNotBlank(reportDTO.getOaid())) {
            params.put("oaid_md5", SecureUtil.md5(reportDTO.getOaid()));
        }
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback_url",callBackUrl);

        String parms = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
       return accountDTO.getDspReportUrl()+"&"+parms;
    }



    public static String handBaiduWphUrl(BaiduRequest baiduRequest,ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new LinkedHashMap<>();
        params.put("imei_md5", StringUtils.isNotBlank(baiduRequest.getImei())?baiduRequest.getImei():"");
        params.put("oaid", StringUtils.isNotBlank(baiduRequest.getOaid())?baiduRequest.getOaid():"");
        params.put("oaid_md5", StringUtils.isNotBlank(baiduRequest.getOaidMd5())?baiduRequest.getOaidMd5():"");
        params.put("device_type", 2);
        params.put("click_time",System.currentTimeMillis());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("click_ip",StringUtils.isNotBlank(baiduRequest.getIp())?baiduRequest.getIp():"");
        params.put("user_agent",StringUtils.isNotBlank(baiduRequest.getUser_agent())?baiduRequest.getUser_agent():"");
        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback_url",callBackUrl);
        String parms = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"&"+parms;
    }
}
